package com.china.school.Pointcut的12种用法;

import com.china.school.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


@Aspect
@ComponentScan({"com.china.school"}) //<context:component-scan base-package="xx"/>
//@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAspectJAutoProxy
public class ExecutionTest {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ExecutionTest.class);
        User user = ctx.getBean(User.class);
        user.eat();

    }


     @Before("execution(* com.china.school.pojo.User.eat())")
    public void before(){
        System.out.println("------before---------");
    }

    @After("execution(* com.china.school.pojo.User.eat())")
    public void after(){
        System.out.println("------after---------无论是否异常，都会执行");
    }


    /**

     ProceedingJoinPoint is only supported for around advice
     */
    @Around("execution(* com.china.school.pojo.User.eat())")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("------around---前------" + joinPoint.getClass());

        if(joinPoint.getArgs().length == 0){

            joinPoint.proceed( );
        }else {
            joinPoint.proceed(  );

        }
        System.out.println("------around-----后----" + joinPoint.getClass());

    }


    @AfterReturning("execution(* com.china.school.pojo.User.eat())")
    public void returning( ){
        System.out.println("------after-returning---------方法正常结束执行");
    }
    @AfterThrowing("execution(* com.china.school.pojo.User.eat())")
    public void throwing(){
        System.out.println("------after-throwing---------抛出异常才执行");
    }








}
